# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QWheelEvent
from PySide6.QtWidgets import QWidget, QScroller, QScrollerProperties, QFrame
from qfluentwidgets import SingleDirectionScrollArea


class ScrollAreaBase:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setWidgetResizable(True)
        self.setFrameShape(QFrame.Shape.NoFrame)
        self.enableTransparentBackground()
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)

        scroller = QScroller.scroller(self)
        scroller.grabGesture(self, QScroller.ScrollerGestureType.LeftMouseButtonGesture)
        properties = QScrollerProperties()
        properties.setScrollMetric(QScrollerProperties.ScrollMetric.DecelerationFactor, 0.8)
        scroller.setScrollerProperties(properties)

    def setWidget(self, widget: QWidget):
        super().setWidget(widget)
        widget.setAutoFillBackground(False)


class VerticalScrollArea(ScrollAreaBase, SingleDirectionScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent, Qt.Orientation.Vertical)


class HorizontalScrollArea(ScrollAreaBase, SingleDirectionScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent, Qt.Orientation.Horizontal)

    def wheelEvent(self, event: QWheelEvent):
        event.ignore()
